package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional //多表操作，开启事务
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品，同时添加对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品数据
        dishMapper.insert(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();

        //判断是否选择了口味
        if(flavors!=null && flavors.size()>0){
            //保存菜品的口味数据 需要注意：DishFlavor中dishId是没有数据的，
            // 所以需要手动设置
            Long dishId = dishDto.getId();//获取菜品id
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //1、查询菜品数据 select * from dish where id = ?
        Dish dish = dishMapper.selectById(id);
        //2、查询菜品对应的口味数据 select * from dish_flavor where dish_id = ?
        LambdaQueryWrapper<DishFlavor> dishFlavorLqw = new LambdaQueryWrapper<>();
        dishFlavorLqw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorMapper.selectList(dishFlavorLqw);
        //返回DishDto对象，先new出该对象 DishDto dishDto = new DishDto();
        DishDto dishDto = new DishDto();
        // 把dish菜品数据 赋值/拷贝 给 dishDto对象 BeanUtils.copyProperties
        BeanUtils.copyProperties(dish,dishDto);
        // 设置dishDto的口味数据 dishDto.setFlavors(xxx)
        dishDto.setFlavors(flavors);
        // 返回数据 ;
        return dishDto;
    }

    /**
     * 更新菜品信息，同时更新对应的口味信息
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 1、修改菜品dish表的数据
        dishMapper.updateById(dishDto);
        //取出菜品id Long dishId = dishDto.getId();
        Long dishId = dishDto.getId();
        // 2、修改菜品的口味数据 如何完成？？
        // 2.1 先删除原有的口味数据: delete from dish_flavor where dish_id = ?
        LambdaQueryWrapper<DishFlavor> dishFlavorLqw = new LambdaQueryWrapper<>();
        dishFlavorLqw.eq(DishFlavor::getDishId,dishId);
        dishFlavorMapper.delete(dishFlavorLqw);
        // 2.2 添加菜品口味数据 List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //封装一下dishId
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //方法一、用mapper实现
        for (DishFlavor flavor : flavors) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setDishId(dishId);
            dishFlavor.setName(dishDto.getName());
            dishFlavor.setValue(flavor.getValue());
            dishFlavorMapper.insert(dishFlavor);
        }
        //方法二、用service实现
        /*for (DishFlavor flavor : flavors) {
            dishFlavorService.save(flavor);
        }*/
    }

}
